package com.example.wuyou.juc;

public class MyThread extends Thread {
    private static final Object lock = new Object();
    public static int piao = 1000;

    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                if (piao <= 0) {
                    System.out.println("窗口" + getName() + "发现票已售罄");
                    break;
                }
                piao--;
                System.out.println("窗口" + getName() + "卖出了一张票，剩余" + piao + "张票");
            }
//            try {
//                // 你可以选择保留或者注释掉这行代码以观察不同的行为
//                Thread.sleep(3000);
//            } catch (InterruptedException e) {
//                System.out.println("窗口" + getName() + "被中断");
//                Thread.currentThread().interrupt(); // 恢复中断状态
//                break;
//            }
        }
        System.out.println("窗口" + getName() + "结束售票");
    }
}

